package com.zp.viewpractice.BasePractice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by izp on 2017/7/27.
 * 三阶贝塞尔曲线
 */
public class BezierTier3 extends View{
    private Paint mPaint;
    private Path mPath;
    private int centerX,centerY;

    private PointF start,end,control1,control2;

    private boolean mode  = true;

    private DashPathEffect dashPathEffect ;
    private CornerPathEffect cornerPathEffect;


    public BezierTier3(Context context) {
        this(context,null);
    }

    public BezierTier3(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPath = new Path();
        mPaint.setColor(Color.BLACK);
        mPaint.setAntiAlias(true);
        mPaint.setPathEffect(new CornerPathEffect(50));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(8);
        mPaint.setTextSize(60);
        start = new PointF(0,0);
        end = new PointF(0,0);
        control1 = new PointF(0,0);
        control2 = new PointF(0,0);
        dashPathEffect = new DashPathEffect(new float[]{8,4},2);
        cornerPathEffect = new CornerPathEffect(50);
    }

    public void setMode(boolean mode) {
        this.mode = mode;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w/2;
        centerY = h/2;

        start.x = centerX-200;
        start.y = centerY;

        end.x = centerX+200;
        end.y = centerY;

        control1.x = centerX;
        control1.y = centerY-100;

        control2.x = centerX;
        control2.y = centerY-100;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();
//        绘制控制点和数据点
        mPaint.setColor(Color.GRAY);
        mPaint.setStrokeWidth(20);

        canvas.drawPoint(start.x, start.y, mPaint);
        canvas.drawPoint(end.x, end.y, mPaint);
        canvas.drawPoint(control1.x, control1.y, mPaint);
        canvas.drawPoint(control2.x, control2.y, mPaint);

//        绘制辅助线
        mPaint.setStrokeWidth(4);
        mPaint.setPathEffect(dashPathEffect);

//        开始到控制点1
        canvas.drawLine(start.x,start.y,control1.x,control1.y,mPaint);

//        控制点1到控制点2
        canvas.drawLine(control1.x,control1.y,control2.x,control2.y,mPaint);
//        控制点2到结束点
        canvas.drawLine(control2.x,control2.y,end.x,end.y,mPaint);


//        绘制贝塞尔曲线
        mPaint.setColor(Color.CYAN);
        mPaint.setStrokeWidth(8);
        mPaint.setPathEffect(cornerPathEffect);
        mPath.moveTo(start.x,start.y);
        mPath.cubicTo(control1.x,control1.y,control2.x,control2.y,end.x,end.y);
        canvas.drawPath(mPath,mPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        return super.onTouchEvent(event);
        if (mode){
            control1.x = event.getX();
            control1.y = event.getY();
        }else {
            control2.x = event.getX();
            control2.y = event.getY();
        }
        invalidate();
        return true;
    }
}
